/**
 * Created by jt on 2017/5/8.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery-1.10.1.min";

import ProjOnListTpl from "./proj_on_list.handlebars";
import ProjOffListTpl from "./proj_off_list.handlebars";
import ProjOnResultsListTpl from "./proj_on_results_list.handlebars";
import ProjOffResultsListTpl from "./proj_off_results_list.handlebars";
import ArticleListTpl from "./article_list.handlebars";
import PartnerListTpl from "./partner_list.handlebars";
import SupplierListTpl from "./supplier_list.handlebars";
import EnquiryListTpl from "./enquiry_list.handlebars";
import EnquiryResultsListTpl from "./enquiry_results_list.handlebars";
import {artclTypCod} from "../config.js";


console.log("***营销公司***",ISMCOMPANY,"*****");
jQuery(function () {

    //首页高亮
    jQuery('.navPart ul li').eq(0).addClass('active');

    //供应商左右跳转
    var suppliers = jQuery('.suppliers');
    if (jQuery('.suppliers .provider').length > 4){
        jQuery('.suppliers >p').show();
    }
    jQuery('.suppliers p.right').click(function () {
        suppliers.find(".provider:first").insertAfter(suppliers.find(".provider:last"));
    });
    jQuery('.suppliers p.left').click(function () {
        suppliers.find(".provider:last").insertAfter(suppliers.find(".provider:first"));
    });

    //头部轮播
    jQuery('.layerslider >div').hide().eq(0).show().children('div').animate({bottom:'0','opacity':100},1000,function () {
        jQuery('.ban1 h2').animate({top:'130px','opacity':100},300);
        jQuery('.ban1 p').animate({width: '430px',height:'104px','opacity':100},1000);
    });
    jQuery('.pageBan .dots li').eq(0).css('backgroundColor','#fff');
    var ban_index = 1;
    var ban_timer = setInterval(ban_fn,5000);
    jQuery('.pageBan .dots li').hover(function () {
        clearInterval(ban_timer);
        if (jQuery(this).css('backgroundColor') != '#fff') {
            ban(this,ban_index == 0? jQuery('.pageBan .dots li').length-1:ban_index - 1);
        }
    },function () {
        ban_index = jQuery(this).index() + 1;
        ban_timer = setInterval(ban_fn,5000);
    });
    function ban(obj,prev) {
        jQuery('.pageBan .dots li').css('backgroundColor','#ffc365');
        jQuery(obj).css('backgroundColor','#fff');

        if (prev == 0) {
            ban2();
        } else if (prev == 1) {
            ban3();
        } else if (prev == 2) {
            ban1();
        }
    }
    function ban_fn() {
        if (ban_index >= jQuery('.pageBan .dots li').length) ban_index = 0;
        ban(jQuery('.pageBan .dots li').eq(ban_index), ban_index == 0? jQuery('.pageBan .dots li').length-1:ban_index - 1);
        ban_index++;
    }

    function ban1() {
        jQuery('.ban3 p').stop().animate({top:'-100px','opacity':0},300);
        jQuery('.ban3 .oil').stop().animate({bottom:'-337px','opacity':0},300,function () {
            jQuery('.ban3').stop().fadeOut('slow');
            jQuery('.ban1').stop().fadeIn(1000,function () {
                jQuery('.ban1 div').stop().animate({bottom:'0','opacity':100},1000,function () {
                    jQuery('.ban1 h2').animate({top:'130px','opacity':100},300);
                    jQuery('.ban1 p').animate({width: '430px',height:'104px','opacity':100},1000);
                });
            });
        });
    }
    function ban2() {
        jQuery('.ban1 h2').stop().animate({top:'-120px','opacity':0},400);
        jQuery('.ban1 p').stop().animate({width: '0',height:'0','opacity':0},500);
        jQuery('.ban1 div').stop().animate({bottom:'-253px','opacity':0},300, function () {
            jQuery('.ban1').stop().fadeOut('slow');
            jQuery('.ban2').stop().fadeIn(1000,function () {
                jQuery('.ban2 p').stop().animate({top:'20px','opacity':100},800,function () {
                    jQuery('.ban2 .people').stop().animate({bottom:'0','opacity':100},1000);
                });
            });
        });
    }
    function ban3() {
        jQuery('.ban2 .people').stop().animate({bottom:'-180px','opacity':0},200);
        jQuery('.ban2 p').stop().animate({top:'-431px','opacity':0},300,function () {
            jQuery('.ban2').stop().fadeOut('slow');
            jQuery('.ban3').stop().fadeIn(1000,function () {
                jQuery('.ban3 .oil').stop().animate({bottom:'0','opacity':100},800,function () {
                    jQuery('.ban3 p').stop().animate({top:'50px','opacity':100},1000);
                });
            });
        });
    }
    
    //加载列表
    var ArticleList = {
        pageNo: 1,
        pageSize: 6,
        maxPage: 1,
        isFirstPage: false,
        isLastPage: false,
        total: 0,
        init: function(){
            this.getArticleData()
                .then(this.renderArticleList);
        },
        getArticleData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                let data = {"artclTypCod":"000001","pubFlg":"1"};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/artcl/queryByConds/${self.pageNo}/${self.pageSize}`,
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表

        renderArticleList: function(resp){
            let self = ArticleList;
            $.each(resp.data.list,function(index,item){
                if (item.pubTim!=null && item.pubTim !='') {
                    item.pubTimFmt = (new Date(item.pubTim)).Format("yyyy-MM-dd");
                } else {
                    item.pubTimFmt = '';
                }
            });
            let resultHtml = ArticleListTpl({ArticleList: resp.data.list});
            $('ul.noticeList').html(resultHtml);
        },


        // 渲染列表
        renderList(){
            this.getArticleData()
                .then(this.renderArticleList);
        }
    };
    ArticleList.init();

    var ProjOnList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getProjOnData()
                .then(this.renderProjOnList);
        },
        getProjOnData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                // let data = {"bidNtcPubFlg":"1"};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/bidProjOnMngForPortal/queryProjs/${self.pageNo}/${self.pageSize}`,
                    type: 'get',
                    dataType: 'json',
                    headers:{'IsMCompany':ISMCOMPANY},
                    // data: JSON.stringify(data),
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表
        renderProjOnList: function(resp){
            let self = ProjOnList;
            // 处理数据begin
            $.each(resp.data.list,function(index,item){
                if (item.ntcPubTim!=null && item.ntcPubTim !='') {
                    item.ntcPubTimFmt = (new Date(item.ntcPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.ntcPubTimFmt = '';
                }

            });
            let resultHtml = ProjOnListTpl({ProjOnList: resp.data.list});
            $('ul.projOnList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getProjOnData()
                .then(this.renderProjOnList);
        }
    };
    ProjOnList.init();

    var ProjOffList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getProjOffData()
                .then(this.renderProjOffList);
        },
        getProjOffData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                // let data = {"bidNtcPubFlg":"1"};
                let data = {};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/bidProjOffMngForPortal/queryByConds/${self.pageNo}/${self.pageSize}`,
                    type: 'post',
                    data: JSON.stringify(data),
                    headers:{'IsMCompany':ISMCOMPANY},
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表

        renderProjOffList: function(resp){
            let self = ProjOffList;
            $.each(resp.data.list,function(index,item){
                if (item.ntcPubTim!=null && item.ntcPubTim !='') {
                    item.ntcPubTimFmt = (new Date(item.ntcPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.ntcPubTimFmt = '';
                }
            });
            let resultHtml = ProjOffListTpl({ProjOffList: resp.data.list});
            $('ul.projOffList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getProjOffData()
                .then(this.renderProjOffList);
        }
    };
    ProjOffList.init();

    var ProjOnResultsList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getProjOnResultsData()
                .then(this.renderProjOnResultsList);
        },
        getProjOnResultsData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                // let data = {"bidRstPubFlg":"1"};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/bidProjOnMngForPortal/queryProjRsts/${self.pageNo}/${self.pageSize}`,
                    type: 'get',
                    // data: JSON.stringify(data)
                    headers:{"IsMCompany":ISMCOMPANY},
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表
        renderProjOnResultsList: function(resp){
            let self = ProjOnResultsList;
            $.each(resp.data.list,function(index,item){
                if (item.rstPubTim!=null && item.rstPubTim !='') {
                    item.rstPubTimFmt = (new Date(item.rstPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.rstPubTimFmt = '';
                }
                if (item.projSts=='-20') {
                    item.projStsFmt="color-red";
                    item.infoFmt="1";
                } else {
                    item.infoFmt="2";
                }
            });
            let resultHtml = ProjOnResultsListTpl({ProjOnResultsList: resp.data.list});
            $('ul.projOnResList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getProjOnResultsData()
                .then(this.renderProjOnResultsList);
        }
    };
    ProjOnResultsList.init();

    var ProjOffResultsList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getProjOffResultsData()
                .then(this.renderProjOffResultsList);
        },
        getProjOffResultsData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                let data = {};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/bidProjOffMngForPortal/queryByRlt/${self.pageNo}/${self.pageSize}`,
                    type: 'post',
                    data: JSON.stringify(data),
                    headers:{'IsMCompany':ISMCOMPANY},
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表
        renderProjOffResultsList: function(resp){
            let self = ProjOffResultsList;
            $.each(resp.data.list,function(index,item){
                if (item.ntcPubTim!=null && item.ntcPubTim !='') {
                    item.ntcPubTimFmt = (new Date(item.ntcPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.ntcPubTimFmt = '';
                }
                if (item.projSts=='-20') {
                    item.projStsFmt="color-red";
                    item.infoFmt="1";
                } else {
                    item.infoFmt="2";
                }
            });
            let resultHtml = ProjOffResultsListTpl({ProjOffResultsList: resp.data.list});
            $('ul.projOffResList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getProjOffResultsData()
                .then(this.renderProjOffResultsList);
        }
    };
    ProjOffResultsList.init();

     // 询价项目列表
    var EnquiryList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getEnquiryListData()
                .then(this.renderEnquiryList);
        },
        getEnquiryListData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                $.ajax({
                    url: HOST_ADDR + `/cpmp/xjProjMngForPortal/queryXjProjs/${self.pageNo}/${self.pageSize}`,
                    type: 'get',
                    dataType: 'json',
                    headers:{'IsMCompany':ISMCOMPANY},
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });
            });
        },
        // 渲染列表
        renderEnquiryList: function(resp){
            let self = EnquiryList;
            $.each(resp.data.list,function(index,item){
                if (item.ntcPubTim!=null && item.ntcPubTim !='') {
                    item.ntcPubTimFmt = (new Date(item.ntcPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.ntcPubTimFmt = '';
                }
            });
            let resultHtml = EnquiryListTpl({EnquiryList: resp.data.list});
            $('ul.enquiryList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getEnquiryListData()
                .then(this.renderEnquiryList);
        }
    };
    EnquiryList.init();

    //  询价结果列表
    var EnquiryResultsList = {
        pageNo: 1,
        pageSize: 6,
        init: function(){
            this.getEnquiryResultsData()
                .then(this.renderEnquiryResultsList);
        },
        getEnquiryResultsData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                $.ajax({
                    url: HOST_ADDR + `/cpmp/xjProjMngForPortal/queryXjProjRsts/${self.pageNo}/${self.pageSize}`,
                    type: 'get',
                    dataType: 'json',
                    headers:{'IsMCompany':ISMCOMPANY},
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });
            });
        },
        // 渲染列表
        renderEnquiryResultsList: function(resp){
            let self = EnquiryResultsList;
            $.each(resp.data.list,function(index,item){
                if (item.rstPubTim!=null && item.rstPubTim !='') {
                    item.rstPubTimFmt = (new Date(item.rstPubTim)).Format("yyyy-MM-dd");
                } else {
                    item.rstPubTimFmt = '';
                }
            });
            let resultHtml = EnquiryResultsListTpl({EnquiryResultsList: resp.data.list});
            $('ul.enquiryResList').html(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getEnquiryResultsData()
                .then(this.renderEnquiryResultsList);
        }
    };
    EnquiryResultsList.init();



    var PartnerList = {
        pageNo: 1,
        pageSize: 10,
        init: function(){
            this.getPartnerData()
                .then(this.renderPartnerList);
        },
        getPartnerData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                let data = {"pubFlg":"1"};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/lnkInf/queryByConds/${self.pageNo}/${self.pageSize}`,
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表

        renderPartnerList: function(resp){
            let self = PartnerList;
            $.each(resp.data.list,function(index,item){
                if (item.pubTim!=null && item.pubTim !='') {
                    item.pubTimFmt = (new Date(item.pubTim)).Format("yyyy-MM-dd");
                } else {
                    item.pubTimFmt = '';
                }
                item.lnkPicFmt = HOST_FILE+item.lnkPic ;
            });
            let resultHtml = PartnerListTpl({PartnerList: resp.data.list});
            $('ul.our-clients').html(resultHtml);
        },


        // 渲染列表
        renderList(){
            this.getPartnerData()
                .then(this.renderPartnerList);
        }
    };
    PartnerList.init();

    var SupplierList = {
        init: function(){
            this.getSupplierData()
                .then(this.renderSupplierList)
                .then(this.bindEvent);
        },
        getSupplierData: function() {
            return new Promise((resolve, reject)=>{
                let self = this;
                let data = {"pubFlg":"1"};
                $.ajax({
                    url: HOST_ADDR + `/cpmp/splrChrm/getSplrChrm`,
                    type: 'get',
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert('系统错误！');
                    }
                });

            });

        },
        // 渲染列表
        renderSupplierList: function(resp){
            let self = SupplierList;
            let finalList = "";
            $.each(resp.data,function(index,item){
                item.indexFmt = index;
                $.each(item.iconItems,function(index,item){
                    item.chrmIconFmt = HOST_FILE+item.chrmIcon;
                });
            });
            let resultHtml = SupplierListTpl({SupplierList: resp.data});
            $('.suppliers').append(resultHtml);
        },

        // 绑定事件
        bindEvent: function(){
            let self = SupplierList;
            for (var i=0; i<$('.provider').length;i++){
                banSup($('.banSup'+i));
            }
        },

        // 渲染列表
        renderList(){
            this.getSupplierData()
                .then(this.renderSupplierList)
                .then(this.bindEvent);
        }

    };
    SupplierList.init();
});

function banSup(cla) {
    cla.children('img').css('opacity','0').eq(0).css('opacity','1');
    var sup_index = 1;
    var sup_timer = setInterval(banSup_fn,4000);
    function ban(obj,prev) {
        cla.children('img').eq(prev).animate({'opacity':'0'},500,function () {
            cla.children('img').eq(obj).animate({'opacity':'1'},500);
        });
    }
    function banSup_fn() {
        if (sup_index >= cla.children('img').length) sup_index = 0;
        ban(sup_index, sup_index == 0? cla.children('img').length-1:sup_index - 1);
        sup_index++;
    }
}
